//
//  PKRootRightViewController.m
//  PianKe
//
//  Created by ybq on 15/12/21.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKRootRightViewController.h"

@interface PKRootRightViewController ()

@property (nonatomic, strong) UITapGestureRecognizer *tap;


@end

@implementation PKRootRightViewController

- (void)dealloc {
    NSLog(@"rightVC 被销毁了");
}

- (void)changeNavViewTitle {
    self.navView.title.text = self.heading;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];

    // Do any additional setup after loading the view.
    
    _navView = [[PKNavigationView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    [_navView.leftButton setImage:[UIImage imageNamed:@"liebiao"] forState:(UIControlStateNormal)];
    [_navView.leftButton addTarget:self action:@selector(changeFrame:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_navView];
//     添加边缘滑动手势,来偏移view的位置
    UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeAction:)];
    screenEdge.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdge];
    
    // 添加手势,点击空白处让view复位
     _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBack:)];
}

- (void)tapBack:(UITapGestureRecognizer *)sender {
    CGRect newFrame = self.navigationController.view.frame;
    newFrame.origin.x = 0;
    self.tableView.userInteractionEnabled = YES;
    self.collectionView.userInteractionEnabled = YES;
    self.scrollView.userInteractionEnabled = YES;
    [UIView beginAnimations:@"changeFrame" context:nil];
    [UIView setAnimationDuration:.3];
    [UIView setAnimationCurve:(UIViewAnimationCurveLinear)];
    self.navigationController.view.frame = newFrame;
    [UIView commitAnimations];
    [self.view removeGestureRecognizer:_tap];
}

// 边缘滑动方法实现
- (void)screenEdgeAction:(UIScreenEdgePanGestureRecognizer *)sender {
    // 获取手势在rootVC.view的位置
    CGFloat x = [sender locationInView:self.navigationController.view.superview].x;
    // 根据手势的位置改变自身view(右侧视图)的位置
    CGRect newFrame = self.navigationController.view.frame;
    // 判断手势点不要超过kWidth - 100
    if (x > kWidth - 100) {
        x = kWidth - 100;
    }
    newFrame.origin.x = x;
    self.navigationController.view.frame = newFrame;
    // 手势结束的时候判断view的位置,大于100回到右侧,小于等于100回到左侧
    if (sender.state == UIGestureRecognizerStateEnded) {
        if (x > 100) {
            newFrame.origin.x = kWidth - 100;
            [self.view addGestureRecognizer:_tap];
            self.tableView.userInteractionEnabled = NO;
            self.collectionView.userInteractionEnabled = NO;
            self.scrollView.userInteractionEnabled = NO;
        } else {
            newFrame.origin.x = 0;
            [self.view removeGestureRecognizer:_tap];
            self.tableView.userInteractionEnabled = YES;
            self.collectionView.userInteractionEnabled = YES;
            self.scrollView.userInteractionEnabled = YES;
        }
        [UIView beginAnimations:@"changeFrame" context:nil];
        [UIView setAnimationDuration:.3];
        [UIView setAnimationCurve:(UIViewAnimationCurveLinear)];
        self.navigationController.view.frame = newFrame;
        [UIView commitAnimations];
    }
}

// 点击方法
- (void)changeFrame:(UIButton *)sender {
    // 改变view的frame
    CGRect newFrame = self.navigationController.view.frame;
    // 如果x为0就偏移到右侧,否则就让view复位
    if (newFrame.origin.x == 0) {
        newFrame.origin.x = kWidth - 100;
        [self.view addGestureRecognizer:_tap];
        self.tableView.userInteractionEnabled = NO;
        self.collectionView.userInteractionEnabled = NO;
        self.scrollView.userInteractionEnabled = NO;
    } else {
        newFrame.origin.x = 0;
        [self.view removeGestureRecognizer:_tap];
        self.tableView.userInteractionEnabled = YES;
        self.collectionView.userInteractionEnabled = YES;
        self.scrollView.userInteractionEnabled = YES;
    }
    // 添加动画效果
//    [UIView animateWithDuration:0.5 animations:^{
//        self.view.frame = newFrame;
//    }];
    [UIView beginAnimations:@"changeFrame" context:nil];
    [UIView setAnimationDuration:.3];
    [UIView setAnimationCurve:(UIViewAnimationCurveLinear)];
    self.navigationController.view.frame = newFrame;
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
